package com.ibiaoke.pay;

import com.ibiaoke.common.configs.AppWeChatConfigs;
import com.ibiaoke.common.utils.IPUtil;
import com.ibiaoke.common.utils.PayCommonUtil;
import com.ibiaoke.common.utils.XMLUtil;
import org.apache.log4j.Logger;
import org.jdom.JDOMException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Component
public class AppWeChatPayClient {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    public Map<String, Object> appWxPay(String body, String out_trade_no, String total_fee, String trade_type, String notify_url,String ip) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        SortedMap<String, String> parameters = new TreeMap<String, String>();
        parameters.put("appid", AppWeChatConfigs.getAppID());
        parameters.put("mch_id", AppWeChatConfigs.getMchId());
        parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
        parameters.put("body", body);
        parameters.put("out_trade_no", out_trade_no);
        parameters.put("fee_type", "CNY");
        parameters.put("total_fee", total_fee);
        parameters.put("spbill_create_ip", ip);
        parameters.put("notify_url", notify_url);
        parameters.put("trade_type", trade_type);
        /**
         * 设置签名
         */
        String sign = PayCommonUtil.createSign("UTF-8", parameters);
        parameters.put("sign", sign);
        /**
         * 封装请求参数结束
         */
        String requestXML = PayCommonUtil.getRequestXml(parameters);
        /**
         * 调用统一下单接口
         */
        String result = PayCommonUtil.httpsRequest(AppWeChatConfigs.UNIFIED_ORDER_URL, "POST", requestXML);
        System.out.println("\n" + result);

        try {
            /**
             * 统一下单接口返回正常的prepay_id，
             * 再按签名规范重新生成签名后，
             * 将数据传输给APP。
             * 参与签名的字段名为 appId，partnerId，prepayId，nonceStr，timeStamp，package。
             * 注意：package的值格式为Sign=WXPay
             */
            Map<String, String> map = XMLUtil.doXMLParse(result);
            SortedMap<String, String> parameterMap2 = new TreeMap<String, String>();
            parameterMap2.put("appid", AppWeChatConfigs.getAppID());
            parameterMap2.put("partnerid", AppWeChatConfigs.getMchId());
            parameterMap2.put("prepayid", map.get("prepay_id"));
            parameterMap2.put("package", "Sign=WXPay");
            parameterMap2.put("noncestr", PayCommonUtil.CreateNoncestr());
            /**
             * 本来生成的时间戳是13位，但是ios必须是10位，所以截取了一下
             */
            parameterMap2.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000).substring(0, 10));
            String sign2 = PayCommonUtil.createSign("UTF-8", parameterMap2);
            parameterMap2.put("sign", sign2);
            resultMap.put("code", "200");
            parameterMap2.put("packageValue",parameterMap2.get("package"));
            resultMap.put("msg", parameterMap2);

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
